/* StoryGenerator.cs

	Infinite Rogue
	By Dan Alexander, Andy Law
	July, 2012

	Cross platform roguelike using C#, Mono, WinForms and GDI.
	
	LICENSE: New BSD License
*/
using System;
using System.Collections.Generic;
using System.IO;

public static class StoryGenerator{

	public static List<string> DungeonNames = new List<string>();
	public static List<string> MaleNames = new List<string>();
	public static List<string> FemaleNames = new List<string>();
	public static List<string> MonsterNames = new List<string>();
	public static List<string> ItemNamesA = new List<string>();
	public static List<string> ItemNamesB = new List<string>();

	//RETURNS A NEW STORY
	public static StoryInfo NewStory(){
	
		if (DungeonNames.Count == 0){
			//load in the resource files
			string section = "";
			var assembly = System.Reflection.Assembly.GetEntryAssembly();
			Stream stream = assembly.GetManifestResourceStream("names.txt") ?? File.Open("names.txt", FileMode.Open);
	
			StreamReader sr = new StreamReader(stream);
		
			string[] lines = sr.ReadToEnd().Split('\n');
			sr.Close();
			
			foreach (string line in lines){
				if (line.StartsWith("#")){
					section = line;
				}
				else{
					if (section.Trim() == "#MONSTERS"){
						MonsterNames.Add(line.Trim());
					}else if (section.Trim() == "#ITEMSA"){
						ItemNamesA.Add(line.Trim());
					}else if (section.Trim() == "#ITEMSB"){
						ItemNamesB.Add(line.Trim());
					}else if (section.Trim() == "#DUNGEONS"){
						DungeonNames.Add(line.Trim());
					}else if (section.Trim() == "#MALENAMES"){
						MaleNames.Add(line.Trim());
					}else if (section.Trim() == "#FEMALENAMES"){
						FemaleNames.Add(line.Trim());
					}
				}
			}
			/*
			Console.WriteLine("DUNGEONS: " + DungeonNames.Count);
			Console.WriteLine("MALES: " + MaleNames.Count);
			Console.WriteLine("FEMALES: " + FemaleNames.Count);
			Console.WriteLine("MONSTERS: " + MonsterNames.Count);
			Console.WriteLine("ItemsA: " + ItemNamesA.Count);
			Console.WriteLine("ItemsB: " + ItemNamesB.Count);
			*/
		}
	
		StoryInfo info = new StoryInfo();
	
		//GET RANDOM GOAL
		int count = Enum.GetNames( typeof( StoryGoal ) ).Length;
		info.goal = (StoryGoal)Utilities.GlobalRandom.Next(count);
		
		//GET RANDOM DUNGEON TYPE
		count = Enum.GetNames( typeof( StoryDungeonType ) ).Length;
		info.dungeonType = (StoryDungeonType)Utilities.GlobalRandom.Next(count);
		
		//GET RANDOM BOSS TYPE
		count = Enum.GetNames( typeof( StoryBossType ) ).Length;
		info.bossType = (StoryBossType)Utilities.GlobalRandom.Next(count);
		
		//GET RANDOM ITEM TYPE
		count = Enum.GetNames( typeof( StoryItemType ) ).Length;
		info.itemType = (StoryItemType)Utilities.GlobalRandom.Next(count);
		
		//GET THE RANDOM NAMES
		info.dungeonName = GetRandomDungeonName();
		info.itemName = GetRandomItemName();
		if (info.bossType == StoryBossType.Creature || info.bossType == StoryBossType.Monster){
			info.bossName = GetRandomMonsterName();
		}else if (info.bossType == StoryBossType.DarkQueen){
			info.bossName = GetRandomFemaleName();
		}else{
			info.bossName = GetRandomMaleName();
		}
		
		//CREATE STORY
		info.story = "Your story begins, brave Player, in the " + info.dungeonType.ToString() + " of " + info.dungeonName + ".\n\n";
		
		if (info.goal == StoryGoal.Kill){
			if (info.bossType == StoryBossType.Creature || info.bossType == StoryBossType.Monster)
			{
				info.story += "For years your village has been terrorized by the [BOSSTYPE] called [BOSSNAME]. You have been chosen by the village elders to save your people from [BOSSNAME]. You must find your way through the [DUNGEONTYPE] of [DUNGEONNAME] and kill [BOSSNAME].";
			}else{
				info.story += "For years your village has been tormented by the [BOSSTYPE] [BOSSNAME]. You have been chosen by the village elders to rid your people of [BOSSNAME]'s evil reign. You must find your way through the [DUNGEONTYPE] of [DUNGEONNAME] and kill [BOSSNAME].";
			}
		}
		else if (info.goal == StoryGoal.Escape){
			info.story += "The war had been tough on your people, and you had been conscripted at a young age to fill in the ranks of the army. At the last battle you were wounded. As blackness set in you thought you were dying... to your dismay, you have awakened to find that you have not. You are a prisoner in the [DUNGEONTYPE] of [DUNGEONNAME]. Now, you must fight your way to your freedom if you ever want to see your home and family again. Your escape will be difficult, for you have heard that the [BOSSTYPE] [BOSSNAME] guards the exit.";
		}else if (info.goal == StoryGoal.Find){
			info.story += "The evil [BOSSTYPE] [BOSSNAME] has stolen your family's heirloom, the [ITEMNAME]. The task has been given to you to enter the [DUNGEONTYPE] of [DUNGEONNAME], find [BOSSNAME], and take back your [ITEMNAME]!";
		}
		
		string boss = info.bossType.ToString();
		if (boss == "DarkKing")
			boss = "Dark King";
		else if (boss == "DarkQueen")
			boss = "Dark Queen";
		
		info.story = info.story.Replace( "[BOSSTYPE]", boss);
		info.story = info.story.Replace( "[BOSSNAME]", info.bossName );
		info.story = info.story.Replace( "[ITEMNAME]", info.itemName );
		info.story = info.story.Replace( "[DUNGEONTYPE]", info.dungeonType.ToString() );
		info.story = info.story.Replace( "[DUNGEONNAME]", info.dungeonName );
		
		return info;
	}

	//RETURNS A RANDOM MONSTER NAME FROM THE MONSTER NAMES FILE
	public static string GetRandomMonsterName(){	
		return MonsterNames[Utilities.GlobalRandom.Next(MonsterNames.Count)];
	}
	
	//RETURNS A RANDOM MALE NAME FROM THE MALE NAMES FILE
	public static string GetRandomMaleName(){
		return MaleNames[Utilities.GlobalRandom.Next(MaleNames.Count)];
	}
	
	//RETURNS A RANDOM FEMALE NAME FROM THE FEMALE NAMES FILE
	public static string GetRandomFemaleName(){
		return FemaleNames[Utilities.GlobalRandom.Next(FemaleNames.Count)];
	}
	
	//RETURNS A RANDOM DUNGEON NAME FROM THE DUNGEON NAMES FILE
	public static string GetRandomDungeonName(){
		return DungeonNames[Utilities.GlobalRandom.Next(DungeonNames.Count)];
	}
	
	//RETURNS A RANDOM ITEM NAME FROM THE 2 ITEM FILES
	public static string GetRandomItemName(){
		return ( ItemNamesA[Utilities.GlobalRandom.Next(ItemNamesA.Count)] + " " + ItemNamesB[Utilities.GlobalRandom.Next(ItemNamesB.Count)] );
	}
}



public struct StoryInfo{
	public StoryGoal goal;
	public StoryDungeonType dungeonType;
	public StoryBossType bossType;
	public StoryItemType itemType;
	public string story;
	public string dungeonName;
    public string bossName;
  	public string itemName;
}

public enum StoryGoal {
    Kill,
    Find,
    Escape,
}

public enum StoryDungeonType {
    Dungeon,
    Tower,
}

public enum StoryBossType{
    Wizard,
    Creature,
    Monster,
    Necromancer,
    DarkKing,
    DarkQueen,
}

public enum StoryItemType{
    Amulet,
    Potion,
    Ring ,
    Sword,
}